package editorsvg.editor.vista;

import editorsvg.editor.documento.ElementoEditable;
import editorsvg.editor.documento.ElementoGrafico;
import editorsvg.gui.generico.AbstractTreeModel;
import editorsvg.gui.generico.BuscadorNodoArbol;
import editorsvg.gui.generico.RestauradorArbol;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 * @author  Jorge Berjano
 */
public class PanelArbolElementos extends javax.swing.JPanel {
    private Observador observador;
    private ElementoEditable documento;
    private RestauradorArbol restauradorArbol;
   
    public interface Observador {
        void seleccionado(ElementoGrafico elemento);
    }

    public PanelArbolElementos() {
        initComponents();       
        arbolElementos.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                if (observador != null) {
                    observador.seleccionado((ElementoGrafico) e.getPath().getLastPathComponent());
                }
            }
        });
        arbolElementos.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        
        restauradorArbol = new RestauradorArbol(arbolElementos);
    }
    
    public void setObservador(Observador observador) {        
       this.observador = observador;
    }
    
    public void setDocumento(ElementoEditable documento) {
        this.documento = documento;
        arbolElementos.setModel(new Modelo());
    }
    
    public void seleccionar(ElementoGrafico elemento) {
        if (elemento == null) {
            arbolElementos.getSelectionModel().clearSelection();
            return;
        }
        BuscadorNodoArbol buscador = new BuscadorNodoArbol(arbolElementos);
        TreePath treePath = buscador.buscar(elemento);
        if (treePath != null) {
            arbolElementos.getSelectionModel().setSelectionPath(treePath);
        }
                
    }
    
    public void actualizar() {
        restauradorArbol.guardarEstado();
        
        if (arbolElementos.getModel() instanceof Modelo) {
            ((Modelo) arbolElementos.getModel()).actualizar();
        }
        
        restauradorArbol.restaurarEstado();
    }
    
    private class Modelo extends AbstractTreeModel {

        @Override
        public Object getRoot() {
            return documento;
        }

        @Override
        public Object getChild(Object parent, int index) {
            if (parent instanceof ElementoEditable) {
                return ((ElementoEditable) parent).getListaElementos().getLista().get(index);
            } else {
                return null;
            }
        }

        @Override
        public int getChildCount(Object parent) {
             if (parent instanceof ElementoEditable) {
                return ((ElementoEditable) parent).getListaElementos().getLista().size();
            } else {
                 return 0;
            }
        }

        @Override
        public boolean isLeaf(Object node) {
            if (node instanceof ElementoEditable) {
                return ((ElementoEditable) node).getListaElementos().getLista().size() == 0;
            } else {
                return false;
            }
        }

        @Override
        public void valueForPathChanged(TreePath path, Object newValue) {            
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        arbolElementos = new javax.swing.JTree();

        setLayout(new java.awt.BorderLayout());

        arbolElementos.setModel(null);
        jScrollPane1.setViewportView(arbolElementos);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree arbolElementos;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
